package com.ruoyi.module.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.module.domain.Footprint;
import com.ruoyi.module.domain.Goods;
import com.ruoyi.module.mapper.EvaluateMapper;
import com.ruoyi.module.mapper.FootprintMapper;
import com.ruoyi.module.mapper.GoodsMapper;
import com.ruoyi.module.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商品Service业务层处理
 *
 * @author fubojin
 * @date 2024-12-06
 */
@Service
public class GoodsServiceImpl implements IGoodsService
{
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private FootprintMapper footprintMapper;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public Goods selectGoodsById(Long id)
    {
        Goods goods = goodsMapper.selectGoodsById(id);
        goods.setEvaluate(evaluateMapper.selectEvaluateSum(id));
        return goods;
    }

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public Goods selectGoodsAndFootById(Long id,Long userId)
    {
        Goods goods = goodsMapper.selectGoodsById(id);
        goods.setEvaluate(evaluateMapper.selectEvaluateSum(goods.getId()));

        Footprint footprint = new Footprint();
        footprint.setUserId(userId);
        footprint.setGoodsId(goods.getId());
        footprint.setCreateTime(DateUtils.getNowDate());
        footprint.setGoodsName(goods.getGoodsName());
        footprint.setGoodsImg(goods.getMainImg());
        footprintMapper.insertFootprint(footprint);
        return goods;
    }

    /**
     * 查询商品列表
     *
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        goods.setDelstatus(0);
        return goodsMapper.selectGoodsList(goods);
    }
    /**
     * 查询按照销量排序商品列表
     */
    @Override
    public List<Goods> listSalesVolumeAll()
    {
        return goodsMapper.listSalesVolumeAll();
    }

    /**
     * 新增商品
     *
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int insertGoods(Goods goods)
    {
        goods.setCreateTime(DateUtils.getNowDate());
        goods.setIsPoints(0);
        goods.setDelstatus(0);
        return goodsMapper.insertGoods(goods);
    }

    /**
     * 修改商品
     *
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int updateGoods(Goods goods)
    {
        goods.setUpdateTime(DateUtils.getNowDate());
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {

        for (Long id : ids) {
            Goods goodsById = goodsMapper.selectGoodsById(id);
            goodsById.setDelstatus(1);
            goodsMapper.updateGoods(goodsById);
        }

        return 1;
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id)
    {
        return goodsMapper.deleteGoodsById(id);
    }
}
